<template>
  <div class="page flex flex-col gap-y-3 p-3 h-dvh">
    <n-card :bordered="false">
      <n-form
        label-align="left"
        label-placement="left"
        label-width="auto"
        :inline="true"
        :model="formParams"
        :show-feedback="false"
        :show-require-mark="false"
      >
        <n-grid responsive="screen" cols="1 s:1 m:2 l:3 xl:4 2xl:4" :collapsed="isCollapse" :x-gap="16" :y-gap="16">
          <n-form-item-gi label="接口名称"> <n-input v-model:value="formParams.apiNm" placeholder="请输入接口名称" /> </n-form-item-gi>
          <n-form-item-gi label="网络地址"> <n-input v-model:value="formParams.path" placeholder="请输入网络地址" /> </n-form-item-gi>
          <n-form-item-gi label="方法"> <n-input v-model:value="formParams.methd" placeholder="请输入方法" /> </n-form-item-gi>
          <n-form-item-gi label="请求总数"> <n-input v-model:value="formParams.reqTotl" placeholder="请输入请求总数" /> </n-form-item-gi>
          <n-form-item-gi label="最大耗时"> <n-input v-model:value="formParams.reqMaxDur" placeholder="请输入请求最大耗时" /> </n-form-item-gi>
          <n-form-item-gi label="分组"> <n-input v-model:value="formParams.grpId" placeholder="请输入分组编号" /> </n-form-item-gi>
          <n-form-item-gi label="应用"> <n-input v-model:value="formParams.appCd" placeholder="请输入应用代码" /> </n-form-item-gi>
          <n-gi :suffix="true">
            <n-space align="center" justify="end">
              <n-button
                v-if="hasPermission('bmpch:web:api:search', '查询Wep接口管理列表')"
                type="primary"
                :loading="searchBtnLoading"
                @click="handleSearch"
              >
                <template #icon>
                  <n-icon>
                    <SearchOutlined />
                  </n-icon>
                </template>
                查询
              </n-button>
              <n-button @click="() => (formParams = new FormParams())">重置</n-button>
              <n-button type="primary" icon-placement="right" text @click="() => (isCollapse = !isCollapse)">
                <template #icon>
                  <n-icon size="14" v-if="isCollapse">
                    <DownOutlined />
                  </n-icon>
                  <n-icon size="14" v-else>
                    <UpOutlined />
                  </n-icon>
                </template>
                {{ isCollapse ? '展开' : '收起' }}
              </n-button>
            </n-space>
          </n-gi>
        </n-grid>
      </n-form>
    </n-card>
    <n-card :bordered="false" class="flex-1">
      <DataTable
        remote
        striped
        :row-key="(row: BMPCH.WebApiQryOutput_WebApiDto) => row.apiId"
        :columns="tableColumns"
        :data="dataList"
        :pagination="pagination"
        :loading="dataLoading"
        :scroll-x="1500"
        @update:page="updatePage"
        @update:page-size="updatePageSize"
        @reload="loadData"
      >
        <template #title>
          <n-space align="center">
            <n-button v-if="hasPermission('bmpch:web:api:add')" type="primary" @click="handleAdd">
              <template #icon>
                <n-icon>
                  <PlusOutlined />
                </n-icon>
              </template>
              新建
            </n-button>
          </n-space>
        </template>
      </DataTable>
      <Edit v-model:show="showModal" :biz-id="bizId" @refresh="loadData" />
    </n-card>
  </div>
</template>

<script lang="ts" setup>
import { h, reactive, ref, onMounted } from 'vue';
import { NSpace, NIcon, NButton, NPopconfirm } from 'naive-ui';
import type { DataTableColumns } from 'naive-ui';
import { removeEmptyField } from '@yanfan/common/utils';
import DataTable, { getDefaultPagination } from '@/components/DataTable';
import type * as BMPCH from '@yanfan/bmpch-tsapi/types';
import { webApiQry, webApiRmv } from '@yanfan/bmpch-tsapi/web-api';
import { DownOutlined, UpOutlined, SearchOutlined, PlusOutlined, EditOutlined, DeleteOutlined } from '@vicons/antd';
import { usePermission } from '@yanfan/common/hooks';
import Edit from './Edit.vue';

defineOptions({
  name: 'BmpchWebApiList',
  titl: 'Wep接口管理列表',
});

const { hasPermission } = usePermission();

class FormParams {
  apiNm: string | null = null;
  path: string | null = null;
  methd: string | null = null;
  reqTotl: number | null = null;
  reqMaxDur: number | null = null;
  grpId: string | null = null;
  appCd: string | null = null;
  chnlId: string | null = null;
  sysId: string | null = null;
}

const tableColumns: DataTableColumns<BMPCH.WebApiQryOutput_WebApiDto> = [
  {
    title: 'URL',
    key: 'path',
    width: 200,
    ellipsis: {
      tooltip: true,
    },
  },
  {
    title: '名称',
    key: 'apiNm',
    width: 200,
    ellipsis: {
      tooltip: true,
    },
  },
  { title: '方法', key: 'methd', width: 100 },
  { title: '请求总数', key: 'reqTotl', width: 100 },
  { title: '最大耗时', key: 'reqMaxDur', width: 100 },
  { title: '分组', key: 'grpNm', width: 200 },
  { title: '应用', key: 'appCd', width: 100 },
  { title: '备注', key: 'rmrk' },
  {
    title: '操作',
    key: 'actions',
    width: 140,
    align: 'center',
    fixed: 'right',
    render(row) {
      return h(NSpace, { align: 'center', justify: 'center' }, () => [
        h(
          NButton,
          { type: 'info', text: true, onClick: () => handleEdit(row) },
          { icon: () => h(NIcon, { size: 18 }, () => h(EditOutlined)), default: () => '编辑' },
        ),
        h(
          NPopconfirm,
          { onPositiveClick: () => handleDelete(row) },
          {
            trigger: () =>
              h(NButton, { type: 'error', text: true }, { icon: () => h(NIcon, { size: 18 }, () => h(DeleteOutlined)), default: () => '删除' }),
            default: () => '确定要删除该数据吗？',
          },
        ),
      ]);
    },
  },
];

const showModal = ref(false);
const isCollapse = ref(false);
const formParams = ref(new FormParams());
const searchBtnLoading = ref(false);
const bizId = ref<string | null>(null);
const pagination = reactive(getDefaultPagination());
const dataLoading = ref(false);
const dataList = ref<BMPCH.WebApiQryOutput_WebApiDto[]>([]);

onMounted(() => {
  loadData();
});

function updatePage(page: number) {
  pagination.page = page;
  loadData();
}

function updatePageSize(pageSize: number) {
  pagination.page = 1;
  pagination.pageSize = pageSize;
  loadData();
}

function loadData() {
  dataLoading.value = true;
  webApiQry({ pageIndx: pagination.page, pageSize: pagination.pageSize, ...removeEmptyField(formParams.value) })
    .then((data) => {
      dataList.value = data.list;
      pagination.itemCount = data.totl;
    })
    .finally(() => {
      dataLoading.value = false;
      searchBtnLoading.value = false;
    });
}

function handleSearch() {
  searchBtnLoading.value = true;
  pagination.page = 1;
  loadData();
}

function handleAdd() {
  bizId.value = String();
  showModal.value = true;
}

function handleEdit(row: BMPCH.WebApiQryOutput_WebApiDto) {
  bizId.value = row.apiId;
  showModal.value = true;
}

function handleDelete(row: BMPCH.WebApiQryOutput_WebApiDto) {
  webApiRmv({ apiId: row.apiId }).then(() => {
    window.$message.success('删除成功');
    loadData();
  });
}
</script>

<style lang="scss" scoped></style>
